<?php
namespace app\api\controller;

use app\api\model\User;
use app\api\validate\OrderCreateValidate;
use think\exception\ValidateException;
use app\api\middleware\Auth;
use app\api\model\Order as OrderModel;
use app\BaseController;
class OrderController extends BaseController
{
	protected $middleware = [Auth::class];

	// 立即下单
	public function orderNow(OrderModel $model)
	{
		try {
            $data = $this->request->param();
            validate(OrderCreateValidate::class)->check($data);
  
            $result = $model->createOrder(
            	$this->request->userInfo->id,
            	$data['addressId'],
            	$data['payWay'],
            	$data['expressWay'],
            	$data['invoiceType'],
            	$data['invoiceHeader'],
            	$data['invoiceContent'],
            	isset($data['cartIds'])?$data['cartIds']:[],
            );
            return $this->returnJson(SUCCESS_MESSAGE,$result);
        } catch (ValidateException $e) {
            return $this->returnJson($e->getError());
        }
	}

    public function lists(OrderModel $model)
    {
        $userId = $this->request->userInfo->id;
        $data = $model->where('user_id',$userId)->with(['orderDetail'=>function($query){
            return $query->with('goods');
        }])->select();
        // dump($data);exit;
        return $this->returnJson(SUCCESS_MESSAGE,$data,count($data));
    }
}